#include "material/terrainmatreial.h"


CTerrainMaterial::CTerrainMaterial() :
CMaterial() {

}
CTerrainMaterial::~CTerrainMaterial() {

}
void CTerrainMaterial::Init() {
    CMaterial::Init();

    const char* _strVertexShader = SHADERCODE(
uniform mat4 projectionViewMatrix;
uniform mat4 modelMatrix;
attribute vec3 a_position;
attribute vec4 a_color0;
attribute vec2 a_texcoord0;
varying vec2 v_texcoord0;
varying vec4 v_color0;
void main(){
    gl_Position = projectionViewMatrix * modelMatrix * vec4( a_position, 1.0 );
    v_color0 = a_color0;
    v_texcoord0 = a_texcoord0;
}
);

    const char* _strFragmentShader = SHADERCODE(
uniform sampler2D layerAlphaTexture;
uniform sampler2D diffuseMap[4];
uniform vec4 uvScale;
varying vec2 v_texcoord0;
varying vec4 v_color0;
float tile_scale[8] = float[](1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0);
void main() {
    vec4 _alphaTexture = texture2D(layerAlphaTexture, v_texcoord0);
    vec4 _color0 = vec4( texture2D(diffuseMap[0], v_texcoord0 * tile_scale[int(uvScale.x)]).rgb, _alphaTexture.r );
    vec4 _color1 = vec4( texture2D(diffuseMap[1], v_texcoord0 * tile_scale[int(uvScale.y)]).rgb, _alphaTexture.g );
    vec4 _color2 = vec4( texture2D(diffuseMap[2], v_texcoord0 * tile_scale[int(uvScale.z)]).rgb, _alphaTexture.b );
    vec4 _color3 = vec4( texture2D(diffuseMap[3], v_texcoord0 * tile_scale[int(uvScale.w)]).rgb, _alphaTexture.a );
    vec4 texelColor = _color0;
    texelColor = mix(texelColor, _color1, _color1.a);
    texelColor = mix(texelColor, _color2, _color2.a);
    texelColor = mix(texelColor, _color3, _color3.a);
    gl_FragColor = texelColor * v_color0;
}
    );

    m_hMaterialHandle = Material_Create("TerrainMaterial", (byte*)_strVertexShader, strlen(_strVertexShader), (byte*)_strFragmentShader, strlen(_strFragmentShader));
    m_hProjectionViewMatrixHandle = Material_GetUniform(m_hMaterialHandle, "projectionViewMatrix", EUT_MATRIX44);
    m_hModelMatrixHandle = Material_GetUniform(m_hMaterialHandle, "modelMatrix", EUT_MATRIX44 );
    m_hDiffuseMapHandle = Material_GetUniform( m_hMaterialHandle, "layerAlphaTexture", EUT_SAMPLER );
    m_hvDiffuseMapHandle[0] = Material_GetUniform( m_hMaterialHandle, "diffuseMap[0]", EUT_SAMPLER );
    m_hvDiffuseMapHandle[1] = Material_GetUniform( m_hMaterialHandle, "diffuseMap[1]", EUT_SAMPLER );
    m_hvDiffuseMapHandle[2] = Material_GetUniform( m_hMaterialHandle, "diffuseMap[2]", EUT_SAMPLER );
    m_hvDiffuseMapHandle[3] = Material_GetUniform( m_hMaterialHandle, "diffuseMap[3]", EUT_SAMPLER );
    m_hUVScale = Material_GetUniform( m_hMaterialHandle, "uvScale", EUT_VEC4 );
}

//void CTerrainMaterial::SetUVScale( int a1, int a2, int a3, int a4 ){
//    Vector4 _sInfo = { 0 };
//    _sInfo[ 0 ] = a1;
//    _sInfo[ 1 ] = a2;
//    _sInfo[ 2 ] = a3;
//    _sInfo[ 3 ] = a4;
//    SetUniform4fv( m_hUVScale, _sInfo );
//}
void CTerrainMaterial::SetUserParameValue( uint32 nName, int32 nDataType, void* pData ){
    if( nName == 3 ){
        SetUniform4fv( m_hUVScale, (float*)pData );
    }
}